package com.example.myaccount.fragment;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.example.myaccount.R;
import com.example.myaccount.activity.AddFlowActivity;
import com.example.myaccount.activity.MainActivity;
import com.example.myaccount.component.FlowDialog;
import com.example.myaccount.constant.MyConst;
import com.example.myaccount.database.DatabaseHelper;
import com.example.myaccount.util.ResourceUtil;

import java.util.ArrayList;

public class AddIncomeFragment extends Fragment implements View.OnClickListener{

    private DatabaseHelper databaseHelper;
    private View root;
    private SharedPreferences sp;
    private ArrayList<String> typesList;
    private ArrayList<String> typesIdList;
    private int typeNum,checkedId,colorBlue,colorGrey;
    private FlowDialog flowDialog;


    private ArrayList<LinearLayout> mLlList;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        root = inflater.inflate(R.layout.fragment_add_income, container, false);

        initData();
        initView();

        return root;

    }

    private void initData() {
        databaseHelper = DatabaseHelper.getDefaultDatabaseHelper(getContext());

        sp = getActivity().getSharedPreferences(MyConst.SETTINGS, Context.MODE_PRIVATE);
        if(sp.getInt(MyConst.SETTINGS_TYPES_INCOME_NUM,0)==0){
            SharedPreferences.Editor editor = sp.edit();
            editor.putInt(MyConst.SETTINGS_TYPES_INCOME_NUM,MyConst.SETTINGS_TYPES_INCOME_NUM_DEFAULT);
            String temp = getString(R.string.type_salary)
                    +"-"+getString(R.string.type_parttime)
                    +"-"+getString(R.string.type_bonus)
                    +"-"+getString(R.string.type_giftcash)
                    +"-"+getString(R.string.type_financemanagement)
                    +"-"+getString(R.string.type_other)
                    +"-"+getString(R.string.type_edit);
            editor.putString(MyConst.SETTINGS_TYPES_INCOME,temp);
            editor.putString(MyConst.SETTINGS_TYPES_INCOME_ID,MyConst.SETTINGS_TYPES_INCOME_ID_DEFAULT);
            editor.commit();
        }

        typeNum = sp.getInt(MyConst.SETTINGS_TYPES_INCOME_NUM,0);

        typesList = new ArrayList<>();
        String[] temp = sp.getString(MyConst.SETTINGS_TYPES_INCOME,"").split("-");
        for(String type:temp){
            typesList.add(type);
        }

        typesIdList = new ArrayList<>();
        temp = sp.getString(MyConst.SETTINGS_TYPES_INCOME_ID,"").split("-");
        for(String typeId:temp){
            typesIdList.add(typeId);
        }

        colorBlue = getResources().getColor(R.color.text_color_blue);
        colorGrey = getResources().getColor(R.color.text_color_grey_dark);

        mLlList = new ArrayList<>();
    }

    private void initView() {
        LinearLayout mLl;
        int id;
        for(int i=0;i<typeNum;i++){
//            Log.d(MyConst.TAG,"ll_"+typesIdList.get(i));
            id = ResourceUtil.getResId("ll_"+typesIdList.get(i),R.id.class);
//            Log.d(MyConst.TAG,"id:"+id);
            mLl = root.findViewById(id);
            mLl.setOnClickListener(this);
//            mLlList.add(mLl);
        }
        flowDialog = new FlowDialog(root.getContext(), R.style.BottomDialog, new FlowDialog.PriorityListener() {
            @Override
            public void refreshPriorityUI(Double amount) {
//                DateFormat df = new SimpleDateFormat("yyyy_MM_dd");
//                String date = df.format(Calendar.getInstance().getTime());
                String date = flowDialog.getDate();
                String annotation = flowDialog.getAnnotation();
                amount = Math.abs(amount);
                Log.d(MyConst.TAG,typesIdList.get(checkedId)+" "+date+" "+amount+" "+annotation);
                if(sp.getString(MyConst.SETTINGS_STATE,"").equals(MyConst.SETTINGS_STATE_ADD)){
                    databaseHelper.insert(typesIdList.get(checkedId),date,amount,annotation);
                }else if(sp.getString(MyConst.SETTINGS_STATE,"").equals(MyConst.SETTINGS_STATE_UPDATE)){
                    int flowId = sp.getInt(MyConst.SETTINGS_FLOW_ID,0);
                    databaseHelper.updateFlow(flowId,typesIdList.get(checkedId),date,amount,annotation);
                }
                Intent intent = new Intent(AddFlowActivity.getActivity(), MainActivity.class);
                startActivity(intent);
            }
        });
    }


    @Override
    public void onClick(View v) {
        ImageView mIv;
        TextView mTv;
        int imageId;
        for(int i=0;i<typeNum;i++){
            if(v.getId()==ResourceUtil.getResId("ll_"+typesIdList.get(i),R.id.class)){
                mIv = root.findViewById(ResourceUtil.getResId("iv_"+typesIdList.get(i),R.id.class));
                mTv = root.findViewById(ResourceUtil.getResId("tv_"+typesIdList.get(i),R.id.class));
                if(mTv.getCurrentTextColor()==colorBlue){
                    imageId = ResourceUtil.getResId("ic_"+typesIdList.get(i),R.drawable.class);
                    mIv.setImageResource(imageId);
                    mTv.setTextColor(colorGrey);
                }else{
                    imageId = ResourceUtil.getResId("ic_"+typesIdList.get(i)+"_checked",R.drawable.class);
                    mIv.setImageResource(imageId);
                    mTv.setTextColor(colorBlue);
                }
                mIv = root.findViewById(ResourceUtil.getResId("iv_"+typesIdList.get(checkedId),R.id.class));
                mTv = root.findViewById(ResourceUtil.getResId("tv_"+typesIdList.get(checkedId),R.id.class));
                imageId = ResourceUtil.getResId("ic_"+typesIdList.get(checkedId),R.drawable.class);
                mIv.setImageResource(imageId);
                mTv.setTextColor(colorGrey);
                checkedId=i;
                flowDialog.show();
                return;
            }
        }
    }
}
